package com.shenfc.fxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shenfc.fxzj.constants.RoleTypeEnum;
import com.shenfc.fxzj.domain.PageBean;
import com.shenfc.fxzj.domain.SysRole;
import com.shenfc.fxzj.domain.SysRoleRel;
import com.shenfc.fxzj.mapper.SysRoleMapper;
import com.shenfc.fxzj.mapper.SysRoleRelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.shenfc.fxzj.service.ISysRoleService;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/3 0003.
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements ISysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleRelMapper sysRoleRelMapper;

    @Override
    public PageInfo<SysRole> querySysRolePage(Map<String, Object> map, PageBean pageBean) {
        PageHelper.startPage(pageBean.getPage(), pageBean.getLimit());
        List<SysRole> list = sysRoleMapper.querySysRoleList(map);
        PageInfo<SysRole> pageInfo = new PageInfo<SysRole>(list);
        pageBean.setRecTotal(pageInfo.getTotal());
        return pageInfo;
    }

    @Override
    public List<SysRole> getAllSysRoles() {
        return sysRoleMapper.getAllSysRoles();
    }

    @Override
    public void addRoleMenuRel(Long roleId, Long[] menuIds) {
        if (roleId == null || menuIds == null || menuIds.length < 1) {
            return;
        }
        for (Long menuId : menuIds) {
            SysRoleRel rel = new SysRoleRel();
            rel.setRoleId(roleId);
            rel.setObjId(menuId);
            rel.setRelType(RoleTypeEnum.MENU.getKey());
            sysRoleRelMapper.insertSelective(rel);
        }
    }

    @Override
    public void addRoleBtnRel(Long roleId, Long[] btnIds) {
        if (roleId == null || btnIds == null || btnIds.length < 1) {
            return;
        }
        for (Long btnid : btnIds) {
            SysRoleRel rel = new SysRoleRel();
            rel.setRoleId(roleId);
            rel.setObjId(btnid);
            rel.setRelType(RoleTypeEnum.BTN.getKey());
            sysRoleRelMapper.insertSelective(rel);
        }
    }

    @Override
    @Transactional
    public void addRole(SysRole sysRole, Long[] menuIds, Long[] btnIds) {
        sysRoleMapper.insertSelective(sysRole);
        addRoleMenuRel(sysRole.getId(), menuIds);
        addRoleBtnRel(sysRole.getId(), btnIds);
    }

    @Override
    @Transactional
    public void updateRole(SysRole sysRole, Long[] menuIds, Long[] btnIds) {
        sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        //清除关联关系
        sysRoleRelMapper.deleteByRoleId(sysRole.getId(), RoleTypeEnum.MENU.getKey());
        sysRoleRelMapper.deleteByRoleId(sysRole.getId(), RoleTypeEnum.BTN.getKey());
        addRoleMenuRel(sysRole.getId(), menuIds);
        addRoleBtnRel(sysRole.getId(), btnIds);
    }

    @Override
    public SysRole getSysRoleById(Long id) {
        return sysRoleMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateStatus(Long id, String status) {
        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        sysRole.setStatus(status);
        return sysRoleMapper.updateByPrimaryKeySelective(sysRole) > 0;
    }


}
